// import com.alibaba.druid.sql.ast.*;
// import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey*;
// import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex*;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "SQLAlterTableAddIndex.h"

#include "../../../sql/ast/SQLIndexDefinition.h"
#include "../../../sql/visitor/SQLASTVisitor.h"
#include "../../../sql/ast/SQLName.h"
#include "../../../sql/dialect/mysql/ast/statement/MySqlTableIndex.h"
#include "../../../sql/dialect/mysql/ast/MySqlKey.h"
#include "SQLSelectOrderByItem.h"
#include "../../../utils/StringUtils.h"

SQLAlterTableAddIndex::SQLAlterTableAddIndex()
{
  indexDefinition = SQLIndexDefinition_ptr(new SQLIndexDefinition());
  indexDefinition->setParent(SharedObject(SQLObjectImpl));
}

SQLIndexDefinition_ptr SQLAlterTableAddIndex::getIndexDefinition()
{
  return indexDefinition;
}

// @Override
void SQLAlterTableAddIndex::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLAlterTableAddIndex)))
  {
    if (indexDefinition->getName() != nullptr)
    {
      indexDefinition->getName()->accept(visitor);
    }
    auto it_getColumns = getColumns()->begin();
    for (int i = 0; i < getColumns()->size(); i++)
    {
      std::advance(it_getColumns, i);
      SQLSelectOrderByItem_ptr item = *it_getColumns;
      if (item != nullptr)
      {
        item->accept(visitor);
      }
    }
    auto it_getCovering = getCovering()->begin();
    for (int i = 0; i < getCovering()->size(); i++)
    {
      std::advance(it_getCovering, i);
      SQLName_ptr item = *it_getCovering;
      if (item != nullptr)
      {
        item->accept(visitor);
      }
    }
  }
  visitor->endVisit(SharedObject(SQLAlterTableAddIndex));
}

BOOL_ptr SQLAlterTableAddIndex::isUnique()
{
  return (!indexDefinition->getType()->empty() &&
         StringUtils::iequals(*indexDefinition->getType(), "UNIQUE"))?BOOL::TRUE:BOOL::FALSE;
}

void SQLAlterTableAddIndex::setUnique(BOOL_ptr unique)
{
  indexDefinition->setType(make_string_ptr("UNIQUE"));
}

SQLSelectOrderByItem_list_ptr SQLAlterTableAddIndex::getItems()
{
  return indexDefinition->getColumns();
}

void SQLAlterTableAddIndex::addItem(SQLSelectOrderByItem_ptr item)
{
  if (item != nullptr)
  {
    item->setParent(SharedObject(SQLObjectImpl));
  }
  indexDefinition->getColumns()->push_back(item);
}

SQLName_ptr SQLAlterTableAddIndex::getName()
{
  return indexDefinition->getName();
}

void SQLAlterTableAddIndex::setName(SQLName_ptr name)
{
  indexDefinition->setName(name);
}

string_ptr SQLAlterTableAddIndex::getType()
{
  return indexDefinition->getType();
}

void SQLAlterTableAddIndex::setType(string_ptr type)
{
  indexDefinition->setType(type);
}

string_ptr SQLAlterTableAddIndex::getUsing()
{
  if (indexDefinition->hasOptions())
  {
    return indexDefinition->getOptions()->getIndexType();
  }
  return nullptr;
}

void SQLAlterTableAddIndex::setUsing(string_ptr using_)
{
  indexDefinition->getOptions()->setIndexType(using_);
}

BOOL_ptr SQLAlterTableAddIndex::isKey()
{
  return indexDefinition->isKey();
}

void SQLAlterTableAddIndex::setKey(BOOL_ptr key)
{
  indexDefinition->setKey(key);
}

void SQLAlterTableAddIndex::cloneTo(MySqlTableIndex_ptr x)
{
  indexDefinition->cloneTo(x->getIndexDefinition());
}

void SQLAlterTableAddIndex::cloneTo(MySqlKey_ptr x)
{
  indexDefinition->cloneTo(x->getIndexDefinition());
}

SQLExpr_ptr SQLAlterTableAddIndex::getComment()
{
  if (indexDefinition->hasOptions())
  {
    return indexDefinition->getOptions()->getComment();
  }
  return nullptr;
}

void SQLAlterTableAddIndex::setComment(SQLExpr_ptr comment)
{
  if (comment != nullptr)
  {
    comment->setParent(SharedObject(SQLObjectImpl));
  }
  indexDefinition->getOptions()->setComment(comment);
}

SQLExpr_ptr SQLAlterTableAddIndex::getKeyBlockSize()
{
  if (indexDefinition->hasOptions())
  {
    return indexDefinition->getOptions()->getKeyBlockSize();
  }
  return nullptr;
}

void SQLAlterTableAddIndex::setKeyBlockSize(SQLExpr_ptr keyBlockSize)
{
  indexDefinition->getOptions()->setKeyBlockSize(keyBlockSize);
}

string_ptr SQLAlterTableAddIndex::getParserName()
{
  if (indexDefinition->hasOptions())
  {
    return indexDefinition->getOptions()->getParserName();
  }
  return nullptr;
}

void SQLAlterTableAddIndex::setParserName(string_ptr parserName)
{
  indexDefinition->getOptions()->setParserName(parserName);
}

BOOL_ptr SQLAlterTableAddIndex::isHashMapType()
{
  return indexDefinition->isHashMapType();
}

void SQLAlterTableAddIndex::setHashMapType(BOOL_ptr hashMapType)
{
  indexDefinition->setHashMapType(hashMapType);
}

SQLExpr_ptr SQLAlterTableAddIndex::getOption(long hash64)
{
  return indexDefinition->getOption(hash64);
}

string_ptr SQLAlterTableAddIndex::getDistanceMeasure()
{
  return indexDefinition->getDistanceMeasure();
}

string_ptr SQLAlterTableAddIndex::getAlgorithm()
{
  return indexDefinition->getAlgorithm();
}

void SQLAlterTableAddIndex::addOption(string_ptr name, SQLExpr_ptr value)
{
  indexDefinition->addOption(name, value);
}

SQLAssignItem_list_ptr SQLAlterTableAddIndex::getOptions()
{
  return indexDefinition->getCompatibleOptions();
}

BOOL_ptr SQLAlterTableAddIndex::isGlobal()
{
  return indexDefinition->isGlobal();
}

void SQLAlterTableAddIndex::setGlobal(BOOL_ptr global)
{
  indexDefinition->setGlobal(global);
}

SQLExpr_ptr SQLAlterTableAddIndex::getDbPartitionBy()
{
  return indexDefinition->getDbPartitionBy();
}

void SQLAlterTableAddIndex::setDbPartitionBy(SQLExpr_ptr x)
{
  indexDefinition->setDbPartitionBy(x);
}

SQLExpr_ptr SQLAlterTableAddIndex::getTablePartitionBy()
{
  return indexDefinition->getTbPartitionBy();
}

void SQLAlterTableAddIndex::setTablePartitionBy(SQLExpr_ptr x)
{
  indexDefinition->setTbPartitionBy(x);
}

SQLExpr_ptr SQLAlterTableAddIndex::getTablePartitions()
{
  return indexDefinition->getTbPartitions();
}

void SQLAlterTableAddIndex::setTablePartitions(SQLExpr_ptr x)
{
  indexDefinition->setTbPartitions(x);
}

// @Override
SQLName_list_ptr SQLAlterTableAddIndex::getCovering()
{
  return indexDefinition->getCovering();
}

// @Override
SQLSelectOrderByItem_list_ptr SQLAlterTableAddIndex::getColumns()
{
  return indexDefinition->getColumns();
}
